package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.entity.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117687028";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCLbOGQVqz9k65yP089dHNad1oKGTJyYDPRSPZyIAKH5XjjltVUKpXvJvndegHdla+IWsR+XFtMp45JyPR9YgsT7FLlgIcaqUL2BI+w6r+FqhdEnN8nXS0+37MH6fPdZ/Ib646xhLOtwf6zsZl47KQrCz9DyMe0dXrQgzz266/D6kJBtg3BtlciS/mlPHDrZwER4zsUqS3cdfkdA4nnwj2Jhbg7E43awx3I3MsuPTNeDv3pV4rUIFQ6o9COMY98kgiOqUK5EBxTPFxnUD9F8yavUgqBVbqan4tPVgBWv4D21TH9rc7IRrYaLcRhzfPjzcLBRKULi1cCnCRmHbuAdZ21AgMBAAECggEAPc3PE0eIuYNMcfJkcs57D+AWNqHCvfatIZmhPSe5V3o+I0zKVdvmyLNTUXzy9MGUpPHvZkAUbJ9dsJF251mdpYA9ouRdf1EGkL2m4Hq+2UZzuAN93Bouo09QMjLf2gZoyT2a7E/xwhVbjVYtvO/ROg9rROXsuK7W+TUjwiTprgMzIentEIbcXwZUEktoxgo6msJAbFKVASiBUfRuYfjtD4UCPPGKd22n6Z8O08StM8xPf/K1L2VkO1n5gMoRMPxhMXEC1K5xolmxMWc/Xel8Zm3tE3zf4oEtEQWvV49fex7AYzKgwR9sNGtM/TH4eQQguEoPlVm/Pd4i8ZOxQIp6BQKBgQDFXp07OqS8V2hxk1RSXtgbnV2aZbbt8cBgJZEs8vWWzel4iH4LmNeYWYuwrz2kHWJLOeqBE1zUeGm77gpUq5g40WmNoiefxA1fUFtY4ereKnsatQs/cY0de1Sja2NR7dXQ2o6Baed7Yuwe3NdYiJv2BE2wE+XAG6SWGZwNtdQmwwKBgQC018XdlTH47J2ExMv0jjS+MonP0rAltkOeJOAP1Ew2/dvo1BSp5QEXGKHuzqn30A00krtgW3av9o0eQB91KSLAOQWLkdNX9KkjE+dd2taW0ultx+KlHIgnS2BGVzR3n/JVRIvgW0J/woyQN1jGxgSoYnUn7I7tqR7VP1y0cU4SJwKBgDLY8UD1FsO+8RVxk2O1M4/Jtve0BLDAe4NJqmEeJs83NVLsNyzdr418ZFSckRDa0Rv4S8X9KwlCDFtyNA8kNg9NRBhNElBCvf80QJRhf5TE9PoockFiC6zwvRls7SWKVG9+HwCQPCa5kYNR+djBOQzkhu858RLRDFHvPxj5IzLpAoGAIJapn26dXMA2+pAEOQPPWV7+Gqda+tDHip4WioO1YEuK74AYYS1VbvYPaFtxoaiqWIkAnzKcRsVlVMg1YwWQr5cW+rnCnh0Gxb12eiFPfkpvqkrU+vATURFyPfrFB7GZF+iMVFfduewzLGHs65UPjdCAUDt+3jEVz1PlvtaUPQ0CgYBIh6kKKG+ZiXQ+dFn17WFNabHcVQvRIdF8ViqaZXsMAIPVFFbw4tRQO6131b2YlWtX5/EUyAzpt5iVaiN58/rqtj7yKsGxzdnWIUvUcob8aPJaiy/AoDL3MiaPlqzbnUVWM1Vg75FJde64gmJ7s7L4Q5LtZjGUUi+/3nYHSxSYuA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8StbJx71FriOlAkS9ICh88e8EC9zLTPXdvQVpPuW2dnwoGPYzDAhDPFYfzhB9Rz+Bkf07S0DORJ6lh332+txZDsT4XK7zLd7O9pHB6VfsvG2WxsHAkWoc48N+1nes4bkdldJ639PXhPx0Xxw93uXfIeU0+cWk8fLZagq9h8FTgxW5o+rrjl8NnSr9CiCjurm+mFekvp8uUa9oOwrdW91HxENaX3qS02Bj4C11GDq+QtryYZ+8pJAWOJZvxZ2t1ftkpe10BhwtU7ZMTAxl9y+Cx4G6gO4aOIUbwgv2xArT62PyzRSMvgfCkDAmikKexQP5VNw4mhoqZjtPPuAF8/AywIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    //测试时需要使用外网穿透地址，保证外网可以访问到
    private  String notify_url = "http://member.gulimall.com/payed/notify";;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //自动收单
    private String timeout_express = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        //timeout_express
        //timeout_express用于告诉支付宝，这个订单最长多少时间内支付有效，超过时间就收单
        //订单相对超时时间。
        //该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：
        // 1m～15d。m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）。
        // 该参数数值不接受小数点， 如 1.5h，可转换为 90m。
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
